import { server } from './mock/server';
import { App } from '@/app/app';
import { Http } from '@/http';
import { AxiosResponse } from 'axios';
import { HttpResponse } from '@/core';
import { TestContext } from 'vitest';
import { i18n, setupI18N } from '@/locales';

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterAll(() => server.close());
beforeEach(async (context: TestContext) => {
	// 设置上下文
	const app = App.getInstance();
	context.app = app;
	// 拦截器
	Http.getHttp().interceptors.response.use((response: AxiosResponse) => {
		return new HttpResponse(response.data, response);
	});
	// 设置全局变量
	vi.stubGlobal('App', app);
	// 国际化
	const VueApp = createApp(App);
	await setupI18N(VueApp);
	vi.stubGlobal('i18n', i18n);
});
afterEach(() => server.resetHandlers());
